import urllib.request as ur
import shutil
import os

class HCY() :
    def __init__(self) :
        if os.path.exists(".\\HCY_TMP") == False :
            os.mkdir(".\\HCY_TMP")
        if os.path.exists(".\\HCY_TMP\\__init__.py") == False :
            with open(".\\HCY_TMP\\__init__.py","wb") as TMP :
                pass
            del TMP

    def HCY_Del(self) :
        if os.path.exists(".\\HCY_TMP") == True :
            shutil.rmtree(".\\HCY_TMP")
            os.mkdir(".\\HCY_TMP")
            with open(".\\HCY_TMP\\__init__.py","wb") as TMP :
                pass
            del TMP

    def HCY_Log(self,date,level = "info") :
        if level == "info" :
            date = "info : " + date + "\n"
        del level
        with open("HCY.log","a+") as H :
            H.write(date)
        del date,H

    def HCY_Config(self,search,path) :
        if os.path.exists(path) == False:
            return(None)
        for line in open(path) :
            if line[-1:] == "\n" :
                line = line[:-1]
            if "=" in line :
                sc = line.split('=',1)[0].strip()
                date = line.split('=',1)[1].strip()
                if search == sc :
                    if date == "_None" :
                        return(" ")
                    else :
                        return(date)
                else :
                    del sc,date
        return(None)

    def HCY_Get(self,Cloud_path,Computer_path = None) :
        Cloud_path = Cloud_path.split("-",1)
        if Computer_path == None :
            Computer_path = ".\\HCY_TMP\\" + Cloud_path[1]
        Cloud = "http://coits.top:1030/coits base/"
        S = Cloud_path[0][0]
        C = Cloud_path[0][1:]
        P = Cloud_path[1]
        Cloud = Cloud + S + "/" + C + "/" + P
        del S,C,P,Cloud_path
        try :
            File = ur.urlopen(Cloud)
        except :
            self.HCY_Log("Get Cloud File > 404 Not Found")
            return(False)
        del Cloud
        Data = File.read()
        with open(Computer_path,"wb") as code :
            code.write(Data)
        self.HCY_Log("Get Cloud File > Done")
        del File,Data,code,Computer_path
        return(True)

    def HCY_Retrieval(self,date) :
        self.HCY_Get("A01-index.cot")
        date = self.HCY_Config(date,".\\HCY_TMP\\index.cot")
        self.HCY_Del()
        return(date)

    def HCY_HEA(self,date,key,mode = "encode") :
        index = self.HCY_Retrieval("HEA_Core")
        self.HCY_Get(index)
        del index
        try :
            import HCY_TMP.HEA_Core as EC
        except :
            self.HCY_Del()
            return("Core Error")
        try :
            if mode == "encode" :
                del mode
                HEA = EC.Core()
                date = HEA.encode(date,key)
                self.HCY_Del
                return(date)

            elif mode == "decode" :
                del mode
                HEA = EC.Core()
                date = HEA.decode(date,key)
                self.HCY_Del()
                return(date)
        except :
            self.HCY_Del()
            return("Unknow Error")


if __name__ == "__main__" :
    A = HCY()
    A.HCY_Del()
